package com.film.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.film.entity.Movie;
import com.film.entity.WishMovie;
import com.film.service.MovieService;
import com.film.service.WishMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Array;
import java.sql.Wrapper;
import java.util.List;

/**
 * @author ZhongXuWei
 * @date 2024/11/20 19:17
 */
@RestController
public class WishMovieController {
    @Autowired
    private WishMovieService wishMovieService;
    @Autowired
    private MovieService movieService;

    @GetMapping("/findWishMovie")
    public Object selectAll() {
        List<WishMovie> lists = wishMovieService.selectAll();
        JSONObject jsonObject = new JSONObject();
        if (!lists.isEmpty()) {
            jsonObject.put("code", 1);
            jsonObject.put("lists", lists);
            jsonObject.put("msg", "获取成功！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "获取失败！");
        }
        return jsonObject;
    }

    @PostMapping("/addWishMovie")
    public Object addLike(@RequestBody WishMovie wishMovie) {
        boolean res = wishMovieService.addWishMovie(wishMovie);
        JSONObject jsonObject = new JSONObject();
        if (res) {
            Movie movie = movieService.selectOne(wishMovie.getMovieId());
            movie.setWishNum(movie.getWishNum() + 1);
            movieService.updateMovie(movie);
            jsonObject.put("code", 1);
            jsonObject.put("msg", "添加成功！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "添加失败！");
        }
        return jsonObject;
    }
    /**
     * 处理删除用户心愿电影的操作
     * 该方法通过DeleteMapping注解映射到"/cancelWishMovie"，表示这是一个HTTP DELETE请求
     * 用于取消用户对某部电影的心愿，即从心愿列表中移除电影
     *
     * @param userId 用户ID，标识哪个用户的心愿电影需要被删除
     * @param movieId 电影ID，标识哪部电影需要从心愿列表中删除
     * @return 返回一个JSONObject对象，包含操作结果的代码和消息
     */
    @DeleteMapping("/cancelWishMovie")
    public Object deleteLike(@RequestParam Integer userId, Integer movieId) {
        boolean res = wishMovieService.deleteWishMovie(userId,movieId);
        JSONObject jsonObject = new JSONObject();
        if (res) {
            Movie movie = movieService.selectOne(movieId);
            movie.setWishNum(movie.getWishNum() - 1);
            movieService.updateMovie(movie);
            jsonObject.put("code", 1);
            jsonObject.put("msg", "删除成功！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "删除失败！");
        }
        return jsonObject;
    }

    @GetMapping("/isWishMovie")
    public Object isWishMovie(HttpServletRequest request) {
        Integer userId = Integer.valueOf(request.getParameter("user_id"));
        Integer movieId = Integer.valueOf(request.getParameter("movie_id"));
        WishMovie one = wishMovieService.selectOne(userId,movieId);
        JSONObject jsonObject = new JSONObject();
        if (one != null) {
            jsonObject.put("code", 1);
            jsonObject.put("wishMovie", one);
            jsonObject.put("msg", "获取成功！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "获取失败！");
        }
        return jsonObject;
    }

    @GetMapping("/getUserLikeMovie")
    public Object getUserLike(@RequestParam Integer userId){
        List<WishMovie> list = wishMovieService.selectUserLike(userId);
        JSONObject jsonObject = new JSONObject();
        if (!list.isEmpty()) {
            jsonObject.put("code", 1);
            jsonObject.put("movies", list);
            jsonObject.put("msg", "获取成功！");
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "获取失败！");
        }
        return jsonObject;
    }
    @GetMapping("/getWishhot")
    public Object getWishhot(){
        QueryWrapper<Movie> qw = new QueryWrapper<>();
        qw.orderByDesc("wish_num");
        List<Movie> movielist = movieService.selectAll(qw);

        JSONObject jsonObject = new JSONObject();
        if (movielist != null){
            jsonObject.put("code", 1);
            jsonObject.put("movielist", movielist);
            jsonObject.put("msg", "获取成功！");
        }
        return jsonObject;
    }
}
